new Vue({
  el: '#app',
  data: {
    ossImgUrl: ossImgUrl,//oss图片域名
    url: "",//签约链接
    user_phone: "",//法人手机号
    enp_title: "",//企业名称
    enp_code: "",//企业信用代码
    enp_user: "",//法人
    auth_status: "",//	0 未认证 1 认证中 2 认证失败 3 认证成功（仅适用于大陆）
    djsTime: 59,//倒计时时间
    timer: '',//计时器
    flag: true//标记避免重复发送
  },
  created() {
    // 获取地址栏参数
    if (getUrlParam("user_phone")) {
      local.set('user_phone', getUrlParam("user_phone"));
    }
    this.user_phone = local.get('user_phone');
    // 缓存小程序传来的 phone 参数（请求头用）
    if (getUrlParam("phone")) {
      local.set("phone", getUrlParam("phone"));
    }
  },
  mounted() {
    clearInterval(this.timer);
    this.djs();
    this.queryAuthStatus();
  },
  destroyed() {
    clearInterval(this.timer);
  },
  methods: {
    // 查询法人授权书签署状态
    queryAuthStatus: function () {
      var that = this;
      get("api/enps/auth_book").then(function (res) {
        that.enp_title = res.data.enp_title;
        that.enp_code = res.data.enp_code;
        that.enp_user = res.data.enp_user;
        that.user_phone = res.data.user_phone ? res.data.user_phone : that.user_phone;
        that.url = res.data.url;
        that.auth_status = res.data.auth_status;
        switch (that.auth_status) {
          case 1://认证中
            // 留在当前页
            break;
          case 2://认证失败
            clearInterval(this.timer);
            window.location.href = "legalRz_sb.html";
            break;
          case 3://认证成功
            clearInterval(this.timer);
            window.location.href = "legalRz_tg.html";
            break;
        }
      })
    },
    // 倒计时
    djs: function () {
      var that = this;
      this.timer = setInterval(function () {
        that.djsTime--;
        if (that.djsTime <= 0) {
          clearInterval(that.timer);
          that.flag = false;//倒计时结束
        }
      }, 1000)
    },
    // 重新发送链接
    sendAgain: function () {
      post("api/enps/auth/put", {
        user_phone: this.user_phone.replace(/\s*/g, "")
      }).then(function (res) { })
    },
    // 刷新
    refresh: function () {
      if (!this.flag) {
        this.flag = true;
        this.djsTime = 59;
        this.djs();
        this.queryAuthStatus();
      }
    },
    // 复制url链接
    copyUrl: function () {
      var u = navigator.userAgent;
      if (u.match(/(iPhone|iPod|iPad);?/i)) {//ios
        window.getSelection().removeAllRanges();
        var copyDOM = document.getElementById("copy-pp");//要复制文字的节点
        var range = document.createRange();
        // 选中需要复制的节点
        range.selectNode(copyDOM);
        // 执行选中元素
        window.getSelection().addRange(range);
        // 执行 copy 操作
        var successful = document.execCommand('copy');
        try {
          var msg = successful ? '复制成功' : '复制失败';
          mui.toast(msg);
        } catch (err) {
          console.log(err);
        }
        // 移除选中的元素
        window.getSelection().removeAllRanges();
      }
      if (u.indexOf('Android') > -1) {//Andiord
        var $copyText = document.getElementById("copy-text"),
          $copyPP = document.getElementById("copy-pp");
        $copyText.innerHTML = $copyPP.innerHTML.replace(/<[^>]+>/g, "");
        $copyText.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        mui.toast('复制成功')
      }
      if (u.indexOf('Android') > -1 && u.indexOf('UCBrowser') > -1) { // 安卓系统的UC浏览器
        mui.toast('若点击复制无效,请长按内容手动复制！')
      }
    }
  }
})
